package com.wyh.springbootweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @PostMapping("login")
    public String login(@RequestParam("userName") String name, @RequestParam("password") String password, Model model, HttpSession session) {

        if (!StringUtils.isEmpty(name) && !StringUtils.isEmpty(password) && "admin".equals(name) && "123".equals(password)) {
            session.setAttribute("loginUser",name);
            return "redirect:/index.html";
        } else {
            model.addAttribute("msg", "用户名或者密码错误");
            return "login";
        }
    }

    @GetMapping("/user/logout")
    public  String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login";
    }
}
